﻿using KoolearnSample.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace KoolearnSample
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        #region Members
        private JsonOper Oper;
        #endregion

        #region Construction
        public MainPage()
        {
            this.InitializeComponent();
            Oper = new JsonOper();
        }
        #endregion

        #region Method
        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        //公开课测试
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(Views.Open.Demo));
        }
        //隐藏注册界面
        private void DialogCancelClicked(object sender, RoutedEventArgs e)
        {
            RegisterDialog.IsOpen = false;
            AskDialog.IsOpen = false;
        }
        //点击打开注册
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            RegisterDialog.IsOpen = true;
        }
        //注册
        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var url = ApiAddress.GetRegisterAddress();
            var name = WMName.Text;
            var email = WMEmail.Text;
            var pwd = Pwd.Password;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("user_name", name);
            dic.Add("password", pwd);
            dic.Add("email", email);
            await Oper.PostDataAsync<KoolearnResult>(url,dic,
                async u =>
                {
                    var dialog = new MessageDialog("", Oper.ConvertRegisterResult(u.Uid));
                    await dialog.ShowAsync();
                    App.IsLoginSuccess = true;
                    App.Uid = u.Uid;
                    RegisterDialog.IsOpen = false;
                });
            
        }
        //打开提问窗口
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            AskDialog.IsOpen = true;
        }
        //提问
        private async void Button_Click_5(object sender, RoutedEventArgs e)
        {
            var url = ApiAddress.GetAskAddress();
            var question = WMQuestion.Text;
            //var uid = App.Uid;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("uid", "54596007");
            dic.Add("question", question);
            await Oper.PostDataAsync<KoolearnAskResult>(url, dic,
                async u =>
                {
                    var dialog = new MessageDialog("", Oper.ConvertAskResult(u.AskId));
                    await dialog.ShowAsync();
                    AskDialog.IsOpen = false;
                });
        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(Views.Exam.Demo));
        }

        #endregion
    }
}
